package com.yic.module.mes.service.itemproducerouteextend;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.mes.controller.admin.itemproducerouteextend.vo.*;
import com.yic.module.mes.dal.dataobject.itemproducerouteextend.ItemProduceRouteExtendDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.itemproducerouteextend.ItemProduceRouteExtendConvert;
import com.yic.module.mes.dal.mysql.itemproducerouteextend.ItemProduceRouteExtendMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 产品工艺路线配置 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ItemProduceRouteExtendServiceImpl implements ItemProduceRouteExtendService {

    @Resource
    private ItemProduceRouteExtendMapper itemProduceRouteExtendMapper;

    @Override
    @Transactional
    public void createItemProduceRouteExtend(List<ItemProduceRouteExtendCreateReqVO> createReqVOs, Long itemProduceId) {
        itemProduceRouteExtendMapper.deleteByItemProduceId(itemProduceId);
        itemProduceRouteExtendMapper.insertBatch(ItemProduceRouteExtendConvert.INSTANCE.convertList03(createReqVOs));
    }

    @Override
    public Long createItemProduceRouteExtend(ItemProduceRouteExtendCreateReqVO createReqVO) {
        // 插入
        ItemProduceRouteExtendDO itemProduceRouteExtend = ItemProduceRouteExtendConvert.INSTANCE.convert(createReqVO);
        itemProduceRouteExtendMapper.insert(itemProduceRouteExtend);
        // 返回
        return itemProduceRouteExtend.getId();
    }

    @Override
    public void updateItemProduceRouteExtend(ItemProduceRouteExtendUpdateReqVO updateReqVO) {
        // 校验存在
        validateItemProduceRouteExtendExists(updateReqVO.getId());
        // 更新
        ItemProduceRouteExtendDO updateObj = ItemProduceRouteExtendConvert.INSTANCE.convert(updateReqVO);
        itemProduceRouteExtendMapper.updateById(updateObj);
    }

    @Override
    public void deleteItemProduceRouteExtend(Long id) {
        // 校验存在
        validateItemProduceRouteExtendExists(id);
        // 删除
        itemProduceRouteExtendMapper.deleteById(id);
    }

    private void validateItemProduceRouteExtendExists(Long id) {
        if (itemProduceRouteExtendMapper.selectById(id) == null) {
            throw exception(ITEM_PRODUCE_ROUTE_EXTEND_NOT_EXISTS);
        }
    }

    @Override
    public ItemProduceRouteExtendDO getItemProduceRouteExtend(Long id) {
        return itemProduceRouteExtendMapper.selectById(id);
    }

    @Override
    public List<ItemProduceRouteExtendDO> getItemProduceRouteExtendList(Collection<Long> ids) {
        return itemProduceRouteExtendMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ItemProduceRouteExtendDO> getItemProduceRouteExtendPage(ItemProduceRouteExtendPageReqVO pageReqVO) {
        return itemProduceRouteExtendMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ItemProduceRouteExtendDO> getItemProduceRouteExtendList(ItemProduceRouteExtendExportReqVO exportReqVO) {
        return itemProduceRouteExtendMapper.selectList(exportReqVO);
    }

    @Override
    public List<ItemProduceRouteExtendRespVO> getItemProduceRouteExtendListByItemProduceId(Long itemProduceId) {
        List<ItemProduceRouteExtendDO> itemProduceRouteExtendDOS = itemProduceRouteExtendMapper.selectListByItemProduceId(itemProduceId);
        return ItemProduceRouteExtendConvert.INSTANCE.convertList(itemProduceRouteExtendDOS);
    }

    @Override
    public List<ItemProduceRouteExtendDO> getItemProduceRouteExtendListByItemProduceIds(Collection<Long> itemProduceIds) {
        List<ItemProduceRouteExtendDO> itemProduceRouteExtendDOS = itemProduceRouteExtendMapper.selectListByItemProduceId(itemProduceIds);
        return ItemProduceRouteExtendConvert.INSTANCE.convertList04(itemProduceRouteExtendDOS);
    }

    @Override
    public int deleteItemProduceRouteExtendByItemProduceId(Long itemProduceId) {
        return itemProduceRouteExtendMapper.deleteByItemProduceId(itemProduceId);
    }

}
